<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Think\Model;
use Api\Logic\UserLogic;

/**
 * 分享控制器
 * @author derek <693472824@qq.com>
 */
class UserController extends BaseController {
    
    public function index(){
        
        $userID = $this->checkLogin('User/index');
        
        $userModel = D('UserView');
        $user = $userModel->where(array('user_id'=>$userID))->find();
        
        $this->assign('userModel', $user);
        $this->display();
    }
    
    public function baseInfo(){
        
        $userID = $this->checkLogin('User/baseInfo');
        
        $userModel = D('UserView');
        $user = $userModel->where(array('user_id'=>$userID))->find();
        
        $this->assign('userModel', $user);
        $this->display();
    }
    
    /**
     * 修改个人信息
     */
    public function ajax_update(){
        
        $userID = UserLogic::is_login();
        
        if(empty($userID) || !is_numeric($userID)){
            $this->result['status'] = '-201';
        }else{
        
            $mediaID = I('media_id');
            $sex = I('sex');
            $name = I('name');
            
            if(!empty($mediaID)){
                $orderLogic = D('Orders', 'Logic');
                $result = $orderLogic->ajaxDownLoad($mediaID, 'Attachment');
                
                if(!empty($result['record_id'])){
                    $data['att_id'] = $result['record_id'];
                }
            }
            
            if(!empty($sex)){
                
                $data['sex'] = $sex == '女' ? '2' : '1';
            }
            
            if(!empty($name)){
                $data['username'] = $name;
            }
            
            if(count($data)>0){
                M('User')->where(array('user_id'=>$userID))->save($data);
            }
            
            $this->result['status'] = '1';
        }
        
        $this->ajaxReturn($this->result);
    }
    
    /**
     * 
     */
    public function updateAccount(){
        
        if(IS_AJAX){
            
            $tel = I('tel');
            $validation = I('validation');
            $userID = UserLogic::is_login();
            $account = UserLogic::getAccount($userID);
            
            if(!checkMobile($tel) || !is_numeric($validation)){
                $this->result['status'] = '422';
                $this->ajaxReturn($this->result);
            }
            
            //账号-验证码信息
            $validateInfo = S('validate.'.$tel);
            
            if(!is_array($validateInfo) || $validation <> $validateInfo['validation']){
                $this->result['status'] = '103';
                $this->ajaxReturn($this->result);
            }
            
            if($account != $tel){
                $result = M('User')->where(array('user_id'=>$userID))->setField('account', $tel);
                if($result){
                    $this->result['status'] = '1';
                }else{
                    $this->result['status'] = '0';
                }
            }else{
                $this->result['status'] = '1';
            }
            
            $this->ajaxReturn($this->result);
        }else{
            
            $this->display();
        }
        
    }
    
}
